//
//  HSScoreTableViewController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit
import SVProgressHUD

class HSScoreTableViewController: UITableViewController {
    
    var dataArray:[HSScoreModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.register(HSScoreTableViewCell.self, forCellReuseIdentifier: "scoreCell")
        
        tableView.estimatedRowHeight = 220
        //tableView.rowHeight = UITableViewAutomaticDimension
        loadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    func loadData() {
        let url = "ZH-schoolname-S-getExamScore"
        let key = userInfo["key"] as! String
        
        let param = ["key":key,"id":0] as [String : Any]
        
        HSNetWorkTools.shared.downloadData(url: url, parameters: param) { [weak self] (data, error) in
            guard let data = data as? NSDictionary else {
                SVProgressHUD.showInfo(withStatus: "网络请求错误！")
                return
            }
            
            var dictArr = data["data"] as! [[String:Any]]
            
            for i in 50...70 {
                
                let s = Int(arc4random() % 80) + 20
                
                let num = Int(arc4random() % 100)
                
                var str = "继续努力"
                for _ in 0...num {
                    str += "呀"
                }
                
                let dict1 = ["es_title": "数学单元测试\(i)", "es_createDate": "2016-07-07", "es_teacherName": "老师1", "es_score": s, "es_course": "数学", "es_id": 8, "es_tId": 1, "es_comment": "继续努力\(i)" + str] as [String : Any]
                dictArr.append(dict1)
            }
            
            for dict in dictArr {
                let model = HSScoreModel(dict: dict)
                
                self?.dataArray.append(model)
                
            }
            self?.tableView.reloadData()
        }
        
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let a = dataArray[indexPath.row]
        
        a.isShowComment = !a.isShowComment
        
        tableView.reloadRows(at: [indexPath], with: .none)
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "scoreCell", for: indexPath) as! HSScoreTableViewCell
        cell.model = dataArray[indexPath.row]
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = dataArray[indexPath.row]
        
        return model.cellHeight
    }
    
}
